package com.example.im
import android.os.Message
import android.text.TextUtils
import cc.vv.lkimcomponent.lkim.bean.LKIMMessage
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMChatType
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMDirect
import com.example.util.finallist.*
import com.example.util.im.MessageEvent
import com.example.util.im.MessageListEvent


/**
 * 消息接收刷新
 * @author  chaohaipeng  on 2018/7/17.
 */
class IMReceiveService private constructor(){
    /**
     * 单例
     */
    private object mHolder {
        val instance = IMReceiveService()
    }
    companion object {
        fun getInstance(): IMReceiveService {
            return mHolder.instance
        }
    }

    /**
     * 插入消息到单聊、群聊消息列表缓存
     *
     * @param account
     * @param lkimMessage
     * @param isIMSend    是否为发送消息
     */
    fun sendInsertCache(account: String, lkimMessage: LKIMMessage, isIMSend: Boolean) {
        var account = account
        /**
         * 存储收到消息时的服务器时间,后续时间显示均使用该扩展数据
         */
        lkimMessage.setAttribute(IMExtKey.EXTKEY_TIMESTAMP, lkimMessage.msgTime.toString())
        if (TextUtils.isEmpty(account)) {
            if (LKIMChatType.SingleChat == lkimMessage.lkimChatType && LKIMDirect.RECEIVE == lkimMessage.lkimDirect) {
                account = lkimMessage.fromAccount
            } else {
                account = lkimMessage.toAccount
            }
        }
        val message=Message()
        message.what=SK_200
        val bundle = message.data
        bundle.putParcelable(IM_MESSAGE,lkimMessage)
        bundle.putBoolean(IM_ISSEND,isIMSend)
        MessageEvent.getInstance().onNewMessage(message)
    }

    /**
     * 刷新消息信息
     *
     * @param messageId   消息id
     * @param lkimMessage 消息
     */
    fun sendRefreshMessage(account: String, messageId: String, lkimMessage: LKIMMessage) {
        var account = account
        if (TextUtils.isEmpty(account)) {
            if (LKIMChatType.SingleChat == lkimMessage.lkimChatType && LKIMDirect.RECEIVE == lkimMessage.lkimDirect) {
                account = lkimMessage.fromAccount
            } else {
                account = lkimMessage.toAccount
            }
        }
        val message=Message()
        message.what=SK_201
        val bundle = message.data
        bundle.putString(IM_MESSAGE_ID, messageId)
        bundle.putParcelable(IM_MESSAGE,lkimMessage)
        MessageEvent.getInstance().onNewMessage(message)

    }

    /**
     * 撤销消息
     *
     * @param account
     * @param messageId
     * @param lkimMessage
     */
    fun sendInsertRepeal(account: String, messageId: String, lkimMessage: LKIMMessage) {
        var account = account
        if (TextUtils.isEmpty(account)) {
            if (LKIMChatType.SingleChat == lkimMessage.lkimChatType && LKIMDirect.RECEIVE == lkimMessage.lkimDirect) {
                account = lkimMessage.fromAccount
            } else {
                account = lkimMessage.toAccount
            }
        }

        val message=Message()
        message.what=SK_202
        val bundle = message.data
        bundle.putString(IM_MESSAGE_ID, messageId)
        bundle.putParcelable(IM_MESSAGE,lkimMessage)
        MessageEvent.getInstance().onNewMessage(message)
    }


    /**
     * 删除消息
     *
     * @param account
     * @param messageId
     */
    fun sendDelete(account: String, messageId: String) {
        val message=Message()
        message.what=SK_203
        val bundle = message.data
        bundle.putString(IM_MESSAGE_ID, messageId)
        MessageEvent.getInstance().onNewMessage(message)
    }

    /**
     * 刷新群组信息
     *
     * @param groupId
     */
    fun sendRefreshGroupInfo(groupId: String) {
    }

    /**
     * 刷新会话列表
     */
    fun sendRefreshConversation() {
        MessageListEvent.getInstance().onNewMessage("刷新")

    }
}